//
//  TextLists.m
//  SocialChair
//
//  Created by Kelly Heffner Wilkerson on 9/27/10.
//  Copyright 2010 Decipher Media, LLC. All rights reserved.
//

#import "TextLists.h"


@implementation TextLists

-(NSString*)getFilename {
	NSString *documentDirectoryPath = [Utilities documentDirectoryPath];
	return [documentDirectoryPath stringByAppendingPathComponent:TEXT_LIST_FILENAME];
}
	

-(TextLists*) init {
	self = [super init];
	if (self != nil) {
		
		NSLog(@"reading from %@", [self getFilename]);
		
		theLists = [NSMutableArray arrayWithContentsOfFile:[self getFilename]];
		
		if (theLists == nil) {
			theLists = [NSMutableArray arrayWithCapacity:5];
		}	
	}
	[theLists retain];
	return self;
}

-(void)writeToFile:(BOOL)atomic {
	[theLists writeToFile:[self getFilename] atomically:atomic];
}

-(int)count {
	return [theLists count];
}

-(NSString*)getListNameAtIndex:(int)index {
	return [[theLists objectAtIndex:index] objectForKey:NAME_KEY];
}

-(void)setListName:(NSString*)newName atIndex:(int)index {
	NSDictionary *listEntry = [theLists objectAtIndex:index];
	
	if (![listEntry isKindOfClass:[NSMutableDictionary class]]) {
		listEntry = [NSMutableDictionary dictionaryWithDictionary:listEntry];
		[theLists replaceObjectAtIndex:index withObject:listEntry];
	}
	
	[(NSMutableDictionary *)listEntry setObject:newName forKey:NAME_KEY];
	[self writeToFile:NO];
}

-(NSArray*)getListAtIndex:(int)index {
	return [[theLists objectAtIndex:index] objectForKey:LIST_KEY];
}

-(NSMutableArray*)getMutableListAtIndex:(int)index {
	NSDictionary *listEntry = [theLists objectAtIndex:index];
	NSArray *list = [listEntry objectForKey:LIST_KEY];
	
	if ([list isKindOfClass:[NSMutableArray class]]) {
		return (NSMutableArray*)list;
	} else {
		if (![listEntry isKindOfClass:[NSMutableDictionary class]]) {
			listEntry = [NSMutableDictionary dictionaryWithDictionary:listEntry];
			[theLists replaceObjectAtIndex:index withObject:listEntry];
		}
		
		NSMutableArray *newList = [NSMutableArray arrayWithArray:list];
		[(NSMutableDictionary *)listEntry setObject:newList forKey:LIST_KEY];
		return newList;
	}
}

-(void)deletePersonAtListIndex:(int) index withPersonIndex:(int)i {
	NSMutableArray *list = [self getMutableListAtIndex:index];
	if ([list count] > i) {
		[list removeObjectAtIndex:i];
		[self writeToFile:NO];
	}
}

-(void)addPersonAtListIndex:(int)index withName:(NSString*)name number:(NSString*)number andLabel:(NSString*)label {
	NSMutableArray *newList = [self getMutableListAtIndex:index];
	NSDictionary *personDict = [NSDictionary dictionaryWithObjectsAndKeys:name, PERSON_NAME_KEY, number, PERSON_NUMBER_KEY, label, PERSON_LABEL_KEY, nil];
	[newList addObject:personDict];
	[self writeToFile:NO];
}

-(void)addGroupPicture:(UIImage*)image forIndex:(int)index {
	NSDictionary *listEntry = [theLists objectAtIndex:index];
	
	if (![listEntry isKindOfClass:[NSMutableDictionary class]]) {
		listEntry = [NSMutableDictionary dictionaryWithDictionary:listEntry];
		[theLists replaceObjectAtIndex:index withObject:listEntry];
	}
	
	NSData *data = UIImagePNGRepresentation(image);
	
	[(NSMutableDictionary *)listEntry setObject:data forKey:IMAGE_KEY];
	[self writeToFile:NO];
}

-(UIImage*)getGroupPictureForIndex:(int)index {
	NSData *data = [[theLists objectAtIndex:index] objectForKey:IMAGE_KEY];
	if (data != nil) {
		return [UIImage imageWithData:data];
	} else {
		return nil;
	}
}

#pragma mark -
#pragma mark adding groups
-(int)newGroup {
	NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithCapacity:2];
	NSMutableArray *newMembers = [NSMutableArray arrayWithCapacity:10];
	[newDict setObject:newMembers forKey:LIST_KEY]; // retains newMembers in newDict
	
	NSString *newName = @"Untitled Group";
	[newDict setObject:newName forKey:NAME_KEY];  // retains newName in newDict
	
	[theLists insertObject:newDict atIndex:0]; // retains newDict in theLists
	[self writeToFile:NO];
	return 0;
}

#pragma mark -
#pragma mark deleting groups
-(void)deleteGroupAtIndex:(int)index {
	if(index >= 0 && index < [theLists count]) {
		[theLists removeObjectAtIndex:index];
	}
	[self writeToFile:NO];
}

#pragma mark -
#pragma mark accessing info about group members

-(NSString*)group:(int)index accessWithIndex:(int)i andKey:(NSString*)key {
	if (index < [theLists count]) {
		NSDictionary *listDict = [theLists objectAtIndex:index];
		NSArray *members = [listDict objectForKey:LIST_KEY];
		if (i < [members count]) {
			return [[members objectAtIndex:i] objectForKey:key];
		}
	}
	return nil;	
}

-(NSString*)group:(int)index memberNameAtIndex:(int)i {
	return [self group:index accessWithIndex:i andKey:PERSON_NAME_KEY];
}

-(NSString*)group:(int)index memberNumberAtIndex:(int)i {
	return [self group:index accessWithIndex:i andKey:PERSON_NUMBER_KEY];
}

-(NSString*)group:(int)index memberLabelAtIndex:(int)i {
	return [self group:index accessWithIndex:i andKey:PERSON_LABEL_KEY];
}

-(NSArray*)getNumbersForGroup:(int)index; {
	NSMutableArray *numbers = [NSMutableArray arrayWithCapacity:10];
	
	if (index < [theLists count]) {
		NSDictionary *listDict = [theLists objectAtIndex:index];
		NSArray *members = [listDict objectForKey:LIST_KEY];
		for(NSDictionary *dict in members) {
			[numbers addObject:[dict objectForKey:PERSON_NUMBER_KEY]];
		}
	}
	return numbers;
}

#pragma mark -
#pragma mark This doesn't belong here (image picker singleton)
-(UIImagePickerController*)getPicker {
	if (imagePicker == nil) {
		imagePicker = [[UIImagePickerController alloc] init];
	}
	
	return imagePicker;
}
@end
